<?php

namespace App\Factory;

use App\Entity\Post;

class PostFactory
{

    /**
     * PostFactory constructor.
     */
    public function __construct()
    {
    }

    public function create(string $title, string $body, string $summary = null, string $slug = 'draft'): Post
    {
        $post = new Post();
        $post->setTitle($title);
        $post->setContent($body);
        if ($summary) {
            $post->setSummary($summary);
        } else {
            $post->setSummary($this->sliceBodyToSummary($body));
        }

        $post->setSlug($slug);

        return $post;

    }

    private function sliceBodyToSummary(string $body, int $length = 140)
    {
        return mb_substr(strip_tags($body), 0, $length);
    }
}